<?php
/**
 * @package lib.famteam.phporm
 * @author polikeiji
 *
 */
class ORMapperQuery {

	public static function getCreateTableQuery($object) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		$vars = get_object_vars($object);
		foreach ($vars as $varsName => $varsValue) {
			$createDifinition = FieldLibrary::getColumnType($varsName);
			if (FieldLibrary::isRequiredField($varsName)) {
				$createDifinition .= " NOT NULL";
			}
			$query->addColumn($varsName, $createDifinition);
		}
		return $query->getCreateTableQuery();
	}
	
	public static function getUpdateQuery($object, $connection, $whereQuery = null, $formula = null) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		$vars = get_object_vars($object);
		foreach ($vars as $varsName => $varsValue) {
			if (isset($varsValue) && $varsValue !== null && FieldLibrary::typeCheck($varsName, $varsValue)) {
				$query->addUpdateValue($varsName, ORMapperQuery::convertFieldValue($varsName, $varsValue, $connection));
			}
		}
		if ($formula != null) {
			$query->addUpdateFormula($formula);
		}
		if (isset($whereQuery)) {
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		else {
			$whereQuery = new ORMapperQueryWhere($object);
			$whereQuery->addEqualsAnd("id", $object->id);
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		return $query->getUpdateQuery($connection);
	}
	
	private static function _getInsertQuery($objects, $tableName, $connection, $excludeID, $ignore) {
		$query = new Query($tableName);
		$column = false;
		foreach ($objects as $object) {
			$vars = get_object_vars($object);
			foreach ($vars as $varsName => $varsValue) {
				if (!FieldLibrary::isString($varsName)) {
					$varsValue = trim($varsValue);
				}
				
				if (isset($varsValue) && (!$excludeID || $varsName != 'id')) {
					if (!$column) {
						$query->addInsertColumn($varsName);
					}
					$query->addInsertValue(ORMapperQuery::convertFieldValue($varsName, $varsValue, $connection));
				}
			}
			$query->flushInsertValue();
			$column = true;
		}
		return $query->getInsertQuery($ignore);
	}
	
	public static function getInsertQuery($object, $connection, $excludeID = true, $ignore = false) {
		$objects = is_array($object) ? $object : array($object);
		return ORMapperQuery::_getInsertQuery($objects, DatabaseConfiguration::getTableName($objects[0]), $connection, $excludeID, $ignore);
	}
	
	public static function getDataMoveInsertQuery($object, $tableName, $connection) {
		return ORMapperQuery::_getInsertQuery($object, $tableName, $connection, true);
	}
	
	public static function getDeleteQuery($object, $connection, $whereQuery = null) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		if (isset($whereQuery)) {
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		else if (isset($object->id)) {
			$whereQuery = new ORMapperQueryWhere($object);
			$whereQuery->addEqualsAnd("id", $object->id);
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		return $query->getDeleteQuery($connection);
	}
	
	public static function getSelectQuery($object,
		$connection,
		$whereQuery,
		$orderColumnName = null,
		$limit = null,
		$offset = 0,
		$distinct = false,
		$groupColumnName = null,
		$isForUpdate = false) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		if (isset($whereQuery) && $whereQuery != null) {
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		if (isset($orderColumnName) && $orderColumnName != null) {
			$query->setOrderBy($orderColumnName);
		}
		if (isset($limit) && $limit != null) {
			$query->setLimit($limit, $offset);
		}
		if ($distinct) {
			$query->setDistinct();
		}
		if (isset($groupColumnName) && $distinct != null) {
			$query->setGroupBy($groupColumnName);
		}
		if ($isForUpdate) {
			$query->isForUpdate();
		}
		return $query->getSelectQuery($connection);
	}
	
	public static function getDataMoveSelectQuery($tableName, $connection) {
		$query = new Query($tableName);
		return $query->getSelectQuery($connection);
	}
	
	public static function getCountSelectQuery($object, $connection, $whereQuery = null) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		if ($whereQuery != null) {
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		return $query->getCountSelectQuery($connection);
	}
	
	public static function getMaxSelectQuery($object, $columnName, $connection, $whereQuery) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		if (isset($whereQuery)) {
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		return $query->getMaxSelectQuery($columnName, $connection);
	}
	
	public static function getMinSelectQuery($object, $columnName, $connection, $whereQuery) {
		$query = new Query(DatabaseConfiguration::getTableName($object));
		if (isset($whereQuery)) {
			$query->setWhereQuery($whereQuery->getWhereQueryObject());
		}
		return $query->getMinSelectQuery($columnName, $connection);
	}
	
	public static function convertFieldValue($fieldName, $fieldValue, $connection) {
		return FieldLibrary::isString($fieldName) ?
			Query::getQueryStringValue($fieldValue, $connection) :
				(FieldLibrary::isTimestamp($fieldName) ?
				Query::getDateString($fieldValue) :
				$fieldValue);
	}

}
?>
